package gui_layer;

import java.awt.Point;
import java.util.List;
import java.util.Vector;

import javax.swing.JOptionPane;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;

import model_layer.StaffMember;
import ctr_layer.StaffMemberCtr;

@SuppressWarnings("serial")
public class ManageAccountsGUI extends UniversalManageWindow
{
	private List<StaffMember> all_staff_members;
	private StaffMemberCtr sm_ctr;
	protected String[] column_names = { "id", "Name", "Type", "Email", "Username" };
	
	public ManageAccountsGUI()
	{
		sm_ctr = new StaffMemberCtr();
		prepare_table(new Object[0][5], column_names);
		get_non_deleted_accounts();
		this.setTitle("Manage Accounts");
		customize_box();
		this.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameActivated(InternalFrameEvent arg0)
			{
				dynamically_search();
				
			}
		});
	}
	
	private void customize_box()
	{
		show_deleted_chckbox.setText("inactive accounts");
	}
	
	private void get_non_deleted_accounts()
	{
		all_staff_members = sm_ctr.find_non_deleted_staff_members();
	}
	
	private void get_deleted_accounts()
	{
		all_staff_members = sm_ctr.find_deleted_staff_memebers();
	}

	@Override
	protected void dynamically_search()
	{
		String text = search_field.getText().toLowerCase();
		MyTableModel mtm = (MyTableModel) table.getModel();
		Vector<Object[]> vector = new Vector<Object[]>();
		Object[] data;
		String[] types = { "Employee", "Manager", "Administrator" };
		for(StaffMember sm : all_staff_members)
		{
			String full_name = sm.getFname().toLowerCase() + " " + sm.getLname().toLowerCase();
			if(full_name.startsWith(text) && ((CurrentAccount.get_privilage_level() == 2) && sm.getPrivilage_level() != 2) ||
					(CurrentAccount.get_privilage_level() == 1 && sm.getPrivilage_level() == 0))
			{
				data = new Object[5];
				data[0] = sm.getId();
				data[1] = full_name;
				data[2] = types[sm.getPrivilage_level()];
				data[3] = sm.getEmail();
				data[4] = sm.getLogin();
				vector.add(data);
			}
		}
		Object[][] filling = new Object[vector.size()][8];
		for(int i = 0; i < vector.size(); i++)
		{
			filling[i] = vector.get(i);
		}
		mtm.setDataVector(filling, column_names);
		table.removeColumn(table.getColumnModel().getColumn(0));
	}

	@Override
	protected void show_deleted()
	{
		 get_deleted_accounts();
		 dynamically_search();

	}

	@Override
	protected void show_non_deleted()
	{
		get_non_deleted_accounts();
		dynamically_search();

	}

	@Override
	protected void create_new()
	{
		NewAccountGUI na_gui = new NewAccountGUI(null);
		this.getDesktopPane().add(na_gui);
		Point window_corner = this.getLocation();
		na_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
		na_gui.setVisible(true);
		na_gui.addInternalFrameListener(new InternalFrameAdapter()
		{
			@Override
			public void internalFrameClosing(InternalFrameEvent arg0)
			{
				dynamically_search();
			}
		});

	}

	@Override
	protected void edit()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			NewAccountGUI ea_gui = new NewAccountGUI(sm_ctr.find_staff_member(id));
			this.getDesktopPane().add(ea_gui);
			Point window_corner = this.getLocation();
			ea_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			ea_gui.setVisible(true);
			ea_gui.addInternalFrameListener(new InternalFrameAdapter()
			{
				@Override
				public void internalFrameClosing(InternalFrameEvent arg0)
				{
					dynamically_search();
				}
			});
		}
	}

	@Override
	protected void view()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			AccountDetailsGUI ad_gui = new AccountDetailsGUI(sm_ctr.find_staff_member(id));
			this.getDesktopPane().add(ad_gui);
			Point window_corner = this.getLocation();
			ad_gui.setLocation((int) window_corner.getX() + 15, (int) window_corner.getY() + 15);
			ad_gui.setVisible(true);
		}
	}

	@Override
	protected void delete()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to delete this account? This can be reversed.",
					"Delete account", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(sm_ctr.delete_account(id))
				{
					JOptionPane.showMessageDialog(this, "Account was successfully deleted!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nAccount wasn't deleted", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
	}

	@Override
	protected void restore()
	{
		int row = table.getSelectedRow();
		if(row != -1)
		{
			row = table.convertRowIndexToModel(row);
			int id = (int) table.getModel().getValueAt(row, 0);
			if(JOptionPane.showConfirmDialog(this, "Are you sure you wish to restore this account?",
					"Restore account", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
			{
				if(sm_ctr.restore_account(id))
				{
					JOptionPane.showMessageDialog(this, "Account successfully restored!", "Success", JOptionPane.PLAIN_MESSAGE);
					show_non_deleted();
				}
				else JOptionPane.showMessageDialog(this, "Error!\nAccount wasn't restored", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}

	}

}
